package com.wty.app.bthcar.utils;

import android.content.Context;
import android.content.SharedPreferences;

public class PreferenceUtil {
    public static String DOWN_CODE = "down";
    public static String LEFT_CODE = "left";
    private static String PREFERENCES_NAME = "Preferences";
    public static String RIGHT_CODE = "right";
    public static String STOP_CODE = "stop";
    public static String UP_CODE = "up";
    private static volatile PreferenceUtil sInstance = null;
    private SharedPreferences mSharedPreferences;

    public static synchronized PreferenceUtil getInstance() {
        PreferenceUtil preferenceUtil;
        synchronized (PreferenceUtil.class) {
            if (sInstance == null) {
                throw new RuntimeException("please init first!");
            }
            preferenceUtil = sInstance;
        }
        return preferenceUtil;
    }

    public static void init(Context context) {
        if (sInstance == null) {
            synchronized (PreferenceUtil.class) {
                if (sInstance == null) {
                    sInstance = new PreferenceUtil(context);
                }
            }
        }
    }

    private PreferenceUtil(Context context) {
        this.mSharedPreferences = context.getSharedPreferences(PREFERENCES_NAME, 0);
    }

    public String getLeftCode() {
        return this.mSharedPreferences.getString(LEFT_CODE, "3");
    }

    public String getRightCode() {
        return this.mSharedPreferences.getString(RIGHT_CODE, "4");
    }

    public String getUpCode() {
        return this.mSharedPreferences.getString(UP_CODE, "1");
    }

    public String getDownCode() {
        return this.mSharedPreferences.getString(DOWN_CODE, "2");
    }

    public int getSpeedRate() {
        return this.mSharedPreferences.getInt(Constant.DEFAULT_SPEED_KEY, 150);
    }

    public int getXSpeedRate() {
        return this.mSharedPreferences.getInt(Constant.DEFAULT_X_SPEED_KEY, 150);
    }

    public String getStopCode() {
        return this.mSharedPreferences.getString(STOP_CODE, "0");
    }

    public void writePreferences(String key, String value) {
        this.mSharedPreferences.edit().putString(key, value).commit();
    }

    public void writePreferences(String key, Boolean value) {
        this.mSharedPreferences.edit().putBoolean(key, value.booleanValue()).commit();
    }

    public void writePreferences(String key, int value) {
        this.mSharedPreferences.edit().putInt(key, value).commit();
    }
}
